﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Web.Security;
using ExamAnalysis.Core;

namespace ExamAnalysis
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && !X.IsAjaxRequest)
            {
                UserIP.Text = GetClientIP();
                ShowMenu();
            }
        }        

        /// <summary>
        /// 根据用户权限显示菜单
        /// </summary>
        private void ShowMenu()
        {
            string[] roles = Roles.GetRolesForUser();
            //1.系统首页
            HomeMenuButton.Visible = Core.Rights.HasMenuRightByRoles(roles, (int)Core.Menu.Items.HomeMenu);

            //2.成绩查询
            ScoreMenuButton.Visible = Core.Rights.HasMenuRightByRoles(roles, (int)Core.Menu.Items.ScoreMenu);

            //3.数据录入
            DataInputMenuButton.Visible = Core.Rights.HasMenuRightByRoles(roles, (int)Core.Menu.Items.DataInputMenu);

            //4.数据维护
            DataMaintainMenuButton.Visible = Core.Rights.HasMenuRightByRoles(roles, (int)Core.Menu.Items.DataMaintainMenu);

            //5.成绩分析
            ViewReportsMenuButton.Visible = Core.Rights.HasMenuRightByRoles(roles, (int)Core.Menu.Items.ViewReportsMenu);

            //6.帐号管理
            AccountManageMenuButton.Visible = Core.Rights.HasMenuRightByRoles(roles, (int)Core.Menu.Items.AccountManageMenu);

            //7.系统管理
            RightsMaintainMenuButton.Visible = Core.Rights.HasMenuRightByRoles(roles, (int)Core.Menu.Items.RightsMaintainMenu);

            //8.云桌面
            DesktopMenuButton.Visible = Core.Rights.HasMenuRightByRoles(roles, (int)Core.Menu.Items.DesktopMenu);

            //9.系统帮助
            ViewHelpMenuButton.Visible = Core.Rights.HasMenuRightByRoles(roles, (int)Core.Menu.Items.ViewHelpMenu);

            //10.隐藏边栏
            HideMenuButton.Visible = Core.Rights.HasMenuRightByRoles(roles, (int)Core.Menu.Items.HideMenu);
        }

        private string GetClientIP()
        {
            string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }

            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.UserHostAddress;
            }
            return result;
        }
    }
}